package algo;

import java.util.Arrays;
import java.util.HashSet;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

@SuppressWarnings({"unchecked", "rawtypes"})
public class BipartiteMatcherTest {

	Vertex v1, v2, v3, v4, v5, v6;

	BipartiteMatcher matcher;

	@Before
	public void setUp() throws Exception {
		matcher = new BipartiteMatcher();
		v1 = new Vertex(true, new HashSet<Vertex>(), "v1");
		v2 = new Vertex(true, new HashSet<Vertex>(), "v2");
		v3 = new Vertex(true, new HashSet<Vertex>(), "v3");
		v4 = new Vertex(false, new HashSet<Vertex>(), "v4");
		v5 = new Vertex(false, new HashSet<Vertex>(), "v5");
		v6 = new Vertex(false, new HashSet<Vertex>(), "v6");
	}

	@Test
	public void test1() {
		v1.neighbours.addAll(Arrays.asList(new Vertex[] { v4, v5, v6 }));
		v2.neighbours.addAll(Arrays.asList(new Vertex[] { v4, v5 }));
		v3.neighbours.addAll(Arrays.asList(new Vertex[] { v4 }));

		v4.neighbours.addAll(Arrays.asList(new Vertex[] { v1, v2, v3 }));
		v5.neighbours.addAll(Arrays.asList(new Vertex[] { v1, v2 }));
		v6.neighbours.addAll(Arrays.asList(new Vertex[] { v1 }));

		Vertex v = matcher.getResult(new HashSet<>(Arrays.asList(new Vertex[] {
				v1, v2, v3, v4, v5, v6 })));
		Assert.assertNull(v);
		Assert.assertEquals(v1, v6.match);
		Assert.assertEquals(v6, v1.match);
		Assert.assertEquals(v2, v5.match);
		Assert.assertEquals(v5, v2.match);
		Assert.assertEquals(v3, v4.match);
		Assert.assertEquals(v4, v3.match);

	}

	@Test
	public void test2() {
		v4.neighbours.addAll(Arrays.asList(new Vertex[] { v2, v3 }));
		v2.neighbours.addAll(Arrays.asList(new Vertex[] { v4 }));
		v3.neighbours.addAll(Arrays.asList(new Vertex[] { v4 }));

		Vertex v = matcher.getResult(new HashSet<>(Arrays.asList(new Vertex[] {
				v4, v2, v3 })));
		Assert.assertNotNull(v);
		Assert.assertTrue(v == v2 || v == v3);
	}

}
